package com.ctrw.config;

import com.ctrw.utils.LoginInterceptor;
import com.ctrw.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加工具类中写的登录注册拦截器
        //拦截非法用户
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns( //一下路径拦截器不生效，直接放行
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login"
                ).order(1);
        //拦截所有请求，用来刷新redis中的token的过期时间
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**").order(0);//拦截器order(0)设置拦截器优先级，值越小优先级越高越先执行
    }
}
